package ligne2vie.topos.server.task;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ligne2vie.topos.server.bo.EntityState;
import ligne2vie.topos.server.bo.Summit;
import ligne2vie.topos.server.service.SummitServiceImpl;

/**
 * The class {@link VersionUpgradeTask} allows executing some important actions allowing new version to be correctly
 * deployed. This may be a database cleaning, a database structure modification and or verification.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class VersionUpgradeTask extends HttpServlet {

	/**
	 * Default serial UID.
	 */
	private static final long serialVersionUID = 1119560209262504497L;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		if (!request.getRequestURI().contains("1.0.0")) {
			out.println("Wrong upgrade version.");
			return;
		}

		SummitServiceImpl service = new SummitServiceImpl();
		List<Summit> summits = service._getAllSummits(false);

		out.println("Upgrading summits...");

		for (Summit summit : summits) {
			summit.setState(EntityState.TO_VERIFY);
			service.persist(summit);
			out.println("Summit [" + summit.getName() + "] upgraded.");
		}

		out.close();
	}
}
